package com.example.ploy4j.controller;

import com.example.ploy4j.dto.GitRepositoryConfigDto;
import com.example.ploy4j.service.GitRepositoryConfigService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/api/git-repo-configs")
@RequiredArgsConstructor
public class GitRepositoryConfigController {

    private final GitRepositoryConfigService gitRepositoryConfigService;

    @PostMapping
    @ResponseBody
    public ResponseEntity<GitRepositoryConfigDto> createGitRepositoryConfig(@Valid @RequestBody GitRepositoryConfigDto gitRepositoryConfigDto) {
        GitRepositoryConfigDto createdConfig = gitRepositoryConfigService.createGitRepositoryConfig(gitRepositoryConfigDto);
        return new ResponseEntity<>(createdConfig, HttpStatus.CREATED);
    }

    @GetMapping
    @ResponseBody
    public ResponseEntity<List<GitRepositoryConfigDto>> getAllGitRepositoryConfigsApi() {
        List<GitRepositoryConfigDto> configs = gitRepositoryConfigService.getAllGitRepositoryConfigs();
        return ResponseEntity.ok(configs);
    }

    @GetMapping("/{id}")
    @ResponseBody
    public ResponseEntity<GitRepositoryConfigDto> getGitRepositoryConfigById(@PathVariable Long id) {
        return gitRepositoryConfigService.getGitRepositoryConfigById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @GetMapping("/by-name/{projectName}")
    @ResponseBody
    public ResponseEntity<GitRepositoryConfigDto> getGitRepositoryConfigByProjectName(@PathVariable String projectName) {
        return gitRepositoryConfigService.getGitRepositoryConfigByProjectName(projectName)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @PutMapping("/{id}")
    @ResponseBody
    public ResponseEntity<GitRepositoryConfigDto> updateGitRepositoryConfig(@PathVariable Long id, @Valid @RequestBody GitRepositoryConfigDto gitRepositoryConfigDto) {
        try {
            GitRepositoryConfigDto updatedConfig = gitRepositoryConfigService.updateGitRepositoryConfig(id, gitRepositoryConfigDto);
            return ResponseEntity.ok(updatedConfig);
        } catch (jakarta.persistence.EntityNotFoundException e) {
            return ResponseEntity.notFound().build();
        }
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public ResponseEntity<Void> deleteGitRepositoryConfig(@PathVariable Long id) {
        try {
            gitRepositoryConfigService.deleteGitRepositoryConfig(id);
            return ResponseEntity.noContent().build();
        } catch (jakarta.persistence.EntityNotFoundException e) {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/manage")
    public String manageGitRepoConfigsPage(Model model) {
        List<GitRepositoryConfigDto> configs = gitRepositoryConfigService.getAllGitRepositoryConfigs();
        model.addAttribute("gitRepoConfigs", configs);
        model.addAttribute("newGitRepoConfig", new GitRepositoryConfigDto());
        return "configs/manage-git-repos";
    }

    @PostMapping("/{id}/test-connection")
    @ResponseBody
    public ResponseEntity<?> testGitRepoConnection(@PathVariable Long id) {
        try {
            return ResponseEntity.ok(gitRepositoryConfigService.testConnection(id));
        } catch (jakarta.persistence.EntityNotFoundException e) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND)
                    .body(java.util.Map.of("success", false, "message", "Git仓库配置未找到: " + id));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(java.util.Map.of("success", false, "message", "测试Git仓库连接失败: " + e.getMessage()));
        }
    }
} 